package com.aiyige.page.publish;

import android.animation.Animator;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.model.User;
import com.aiyige.page.login.model.impl.LoginManager;
import com.aiyige.page.publish.info.model.PublishInfoFormModel;
import com.aiyige.page.publish.learnvideo.model.PublishLearnVideoFormModel;
import com.aiyige.page.publish.majorcourse.model.MajorCourseFormModel;
import com.aiyige.page.publish.normalvideo.model.PublishNormalVideoFormModel;
import com.aiyige.page.publish.photo.model.PhotoFormModel;
import com.aiyige.page.publish.traingcard.model.TraingCardFormModel;
import com.aiyige.page.publish.traingclass.model.TraingClassFormModel;
import com.aiyige.utils.AccountUtil;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = ARouterConfig.PublishEntryPage)
public class PublishEntryPage extends AppCompatActivity {
    public static final int REQUEST_CODE_NAV_TO_PUBLISH_PAGE = 1;
    @BindView(R.id.closeBtn)
    ImageView closeBtn;
    @BindView(R.id.buttonPanel)
    LinearLayout buttonPanel;
    Handler mainHandler;
    @BindView(R.id.containerLayout)
    LinearLayout containerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_publish_entry);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        mainHandler = new Handler(Looper.getMainLooper());
        mainHandler.post(new Runnable() {
            @Override
            public void run() {
                containerLayout.animate()
                        .alpha(1)
                        .setDuration(300)
                        .start();
                buttonPanel.animate()
                        .setDuration(600)
                        .translationY(0)
                        .setInterpolator(new OvershootInterpolator(3))
                        .start();
                closeBtn.animate()
                        .setStartDelay(100)
                        .rotation(0)
                        .setDuration(300)
                        .start();
            }
        });
    }

    @OnClick({R.id.publishTraingCardLayout, R.id.publishTraingClassLayout, R.id.closeBtn, R.id.publishPhotoLayout, R.id.publishNormalVideoLayout, R.id.publishInfoLayout, R.id.publishSpecificTopicLayout, R.id.publishLearnVideoLayout, R.id.publishMajorCourseLayout, R.id.publishPersonalTrainerLayout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.closeBtn:
                finish();
                break;
            case R.id.publishPhotoLayout:
                ARouter.getInstance()
                        .build(ARouterConfig.PublishPhotoPage)
                        .withParcelable("formModel", PhotoFormModel.newBuilder().build())
                        .navigation(this, REQUEST_CODE_NAV_TO_PUBLISH_PAGE);
                break;
            case R.id.publishNormalVideoLayout:
                ARouter.getInstance()
                        .build(ARouterConfig.PublishNormalSingleVideoPage)
                        .withParcelable("formModel", PublishNormalVideoFormModel.newBuilder().build())
                        .navigation(this, REQUEST_CODE_NAV_TO_PUBLISH_PAGE);
                break;
            case R.id.publishInfoLayout:
                ARouter.getInstance()
                        .build(ARouterConfig.PublishInfoPage)
                        .withParcelable("formModel", PublishInfoFormModel.newBuilder().build())
                        .navigation(this, REQUEST_CODE_NAV_TO_PUBLISH_PAGE);
                break;
            case R.id.publishSpecificTopicLayout:
                break;
            case R.id.publishLearnVideoLayout:
                ARouter.getInstance()
                        .build(ARouterConfig.PublishLearnVideoPage)
                        .withParcelable("formModel", PublishLearnVideoFormModel.newBuilder().build())
                        .navigation(this, REQUEST_CODE_NAV_TO_PUBLISH_PAGE);
                break;
            case R.id.publishMajorCourseLayout:
                ARouter.getInstance()
                        .build(ARouterConfig.PublishMajorCourseFormPage)
                        .withParcelable("formModel", MajorCourseFormModel.newBuilder().build())
                        .navigation(this, REQUEST_CODE_NAV_TO_PUBLISH_PAGE);
                break;
            case R.id.publishTraingCardLayout:
                ARouter.getInstance()
                        .build(ARouterConfig.PublishTraingCardFormPage)
                        .withParcelable("formModel", TraingCardFormModel.newBuilder().build())
                        .navigation(this, REQUEST_CODE_NAV_TO_PUBLISH_PAGE);
                break;
            case R.id.publishTraingClassLayout:
                ARouter.getInstance()
                        .build(ARouterConfig.PublishTraingClassFormPage)
                        .withParcelable("formModel", TraingClassFormModel.newBuilder().build())
                        .navigation(this, REQUEST_CODE_NAV_TO_PUBLISH_PAGE);
                break;
            case R.id.publishPersonalTrainerLayout:
                break;
            default:
                break;
        }
    }

    /**
     * 检查是否绑定了手机号
     */
    private void checkBindingMobile() {
        if (LoginManager.getInstance().isLogin()) {
            User user = AccountUtil.getCurrentUser();
            if (user != null && TextUtils.isEmpty(user.getMobile())) {
                ARouter.getInstance().build(ARouterConfig.BindingPhonePage).navigation();
            } else {
                finish();
            }
        } else {
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_CODE_NAV_TO_PUBLISH_PAGE:
                setResult(RESULT_OK, data);
                PublishEntryPage.super.finish();
                break;
            default:
                super.onActivityResult(requestCode, resultCode, data);
                break;
        }
    }

    @Override
    public void finish() {
        containerLayout.animate().setDuration(300).setInterpolator(new AccelerateInterpolator()).alpha(0).setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                buttonPanel.animate().setDuration(200).setInterpolator(new AccelerateInterpolator()).translationY(buttonPanel.getMeasuredHeight() * 2).start();
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                PublishEntryPage.super.finish();
                overridePendingTransition(0, 0);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        }).start();
    }
}
